package com.primb.bench.common.module.repository;

import android.text.TextUtils;

import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.home.entity.ClientInfoEntity;
import com.primb.bench.project.home.entity.EnsureInfoEntity;
import com.primb.bench.project.home.entity.HttpClientEntity;
import com.primb.bench.project.home.entity.LoanCalculateInfoEntity;
import com.primb.bench.project.home.entity.LoanInfoEntity;
import com.primb.bench.project.home.entity.LogoInfoEntity;
import com.primb.bench.project.home.entity.RequestClientEntity;
import com.primb.bench.project.home.entity.TaxInfoEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by Chen on 2017/7/12.
 * 功能描述：
 */

public class LoanClientRepository {
    public static Observable<List<ClientInfoEntity>> getLoanClientListEntity(RequestClientEntity entity) {
        HashMap<String, String> map = new HashMap<>();
        map.put("start", entity.getStart() + "");
        map.put("limit", entity.getLimit() + "");

        if (entity.getCustId() != null && !TextUtils.isEmpty(entity.getCustId())) {
            map.put("custId", entity.getCustId());
        }
        if (entity.getCustName() != null && !TextUtils.isEmpty(entity.getCustName())) {
            map.put("custName", entity.getCustName());
        }
        if (entity.getCustType() != null && !TextUtils.isEmpty(entity.getCustType())) {
            map.put("custType", entity.getCustType());
        }
        return ApiClient.initService(ApiService.class).getLoanClientList(map)
                .flatMap(new Func1<HttpClientEntity, Observable<List<ClientInfoEntity>>>() {
                    @Override
                    public Observable<List<ClientInfoEntity>> call(HttpClientEntity httpClientEntity) {
                        List<HttpClientEntity.DataBean> data = httpClientEntity.getData();
                        List<ClientInfoEntity> dataList = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            HttpClientEntity.DataBean dataBean = data.get(i);
                            ClientInfoEntity infoEntity = new ClientInfoEntity();
                            infoEntity.setCustomerName(dataBean.getCustName());
                            infoEntity.setCustomerNum(dataBean.getCustId());
                            infoEntity.setCustomerType(dataBean.getCustTypeDesc());
                            infoEntity.setCustomerTypeId(dataBean.getCustType());
                            infoEntity.setCustomerCredit(dataBean.getCreditRankCdDesc());
                            infoEntity.setCustomerCreditId(dataBean.getCreditRankCd());
                            infoEntity.setCustomerSize(dataBean.getLnsScopeCdDesc());
                            infoEntity.setCustomerSizeId(dataBean.getLnsScopeCd());
                            infoEntity.setCustomerContributionNum(dataBean.getNowRemainGx());
                            infoEntity.setCustomerAdjustRate(dataBean.getPdAdjust());
                            infoEntity.setCustomerIsContribution("0");
//                            if (dataBean.getCustIdM() == null || dataBean.getCustIdM().equals("")) {
//                                infoEntity.setCustomerIsContributionM("0");
//                            } else {
//                                infoEntity.setCustomerIsContributionM("1");
//                            }
                            infoEntity.setCustomerNumM(dataBean.getCustIdM());
                            infoEntity.setCustomerContributionNumM(dataBean.getNowRemainGxM());
                            infoEntity.setCustomerUsedContributionM(dataBean.getNowUseGxM());
                            infoEntity.setCustomerAllContribution(dataBean.getNowSumGx());
                            infoEntity.setCustomerUsedContribution(dataBean.getNowUseGx());
                            infoEntity.setCustomerAllProfit(dataBean.getNowSumGp());
                            infoEntity.setCustomerInfoEmpty(false);
                            dataList.add(infoEntity);
                        }
                        return Observable.just(dataList);
                    }
                })
                .compose(RxSchedulerUtils.normalSchedulersTransformer());
    }
}
